package cn.qzzg.springcloud.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

/**
 * RestTemplate 是 Spring 提供的用于同步client-side HTTP 访问的工具
 */
@Configuration
public class RestClientConfig {

    @Bean
//    @LoadBalanced  //(@LoadBalanced 注解会使得 RestTemplate 能够解析服务名到实际的服务实例地址。)
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        builder.setConnectTimeout(Duration.ofSeconds(5));
        builder.setReadTimeout(Duration.ofSeconds(5));
        return builder.build();
    }
}
